function sortByTitle(desc){
	
	var lists = getElementsByClass("imgItem");

	if(desc)
		lists.sort(sortTitlesDownwards);
	else
		lists.sort(sortTitlesUpwards);
		
	var cont = document.getElementById("imgList");
	for(var i = 0; i < lists.length; i++){
		cont.appendChild(lists[i]);	
	}
}

function sortByDate(desc){
	
	var lists = getElementsByClass("imgItem");
	
	if(desc)
		lists.sort(sortDateDownwards);
	else
		lists.sort(sortDateUpwards);
		
	var cont = document.getElementById("imgList");
	for(var i = 0; i < lists.length; i++){
		cont.appendChild(lists[i]);
	}
}

function sortByCountry(desc){
	
	var lists = getElementsByClass("imgItem");
	
	if(desc)
		lists.sort(sortCountryDownwards);
	else
		lists.sort(sortCountryUpwards);
		
	var cont = document.getElementById("imgList");
	for(var i = 0; i < lists.length; i++){
		cont.appendChild(lists[i]);
	}
}

function getElementsByClass(clase){

	var lista = document.getElementsByTagName("*");
	var nuevaLista = new Array();
	
	for(var i = 0; i < lista.length; i++){
	
		if(lista[i].className == clase){
			nuevaLista.push(lista[i]);
		}
	}

	return nuevaLista;
}

function sortTitlesUpwards(a,b){
	var aTitle = a.getElementsByTagName("h3")[0].innerHTML;
	var bTitle = b.getElementsByTagName("h3")[0].innerHTML;
	
	return aTitle >= bTitle;
}

function sortTitlesDownwards(a,b){
	var aTitle = a.getElementsByTagName("h3")[0].innerHTML;
	var bTitle = b.getElementsByTagName("h3")[0].innerHTML;
	
	return aTitle < bTitle;
}

function sortDateUpwards(a,b){
	var aCadDate = a.getElementsByTagName("li")[1].innerHTML.split(" ")[2];
	var bCadDate = b.getElementsByTagName("li")[1].innerHTML.split(" ")[2];
	
	var aFechaP = aCadDate.split("-");
	var aFecha = new Date(aFechaP[2], aFechaP[1] - 1, aFechaP[0]);
	
	var bFechaP = bCadDate.split("-");
	var bFecha = new Date(bFechaP[2], bFechaP[1] - 1, bFechaP[0]);
	
	return aFecha >= bFecha;
}

function sortDateDownwards(a,b){
	var aCadDate = a.getElementsByTagName("li")[1].innerHTML.split(" ")[2];
	var bCadDate = b.getElementsByTagName("li")[1].innerHTML.split(" ")[2];
	
	var aFechaP = aCadDate.split("-");
	var aFecha = new Date(aFechaP[2], aFechaP[1] - 1, aFechaP[0]);
	
	var bFechaP = bCadDate.split("-");
	var bFecha = new Date(bFechaP[2], bFechaP[1] - 1, bFechaP[0]);
	
	return aFecha < bFecha;
}

function sortCountryUpwards(a,b){
	var aTitle = a.getElementsByTagName("li")[2].innerHTML;
	var bTitle = b.getElementsByTagName("li")[2].innerHTML;
	
	return aTitle >= bTitle;
}

function sortCountryDownwards(a,b){
	var aCountry = a.getElementsByTagName("li")[2].innerHTML;
	var bCountry = b.getElementsByTagName("li")[2].innerHTML;
	
	return aCountry < bCountry;
}